package online.longzipeng.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import online.longzipeng.entity.User;
import online.longzipeng.mapper.UserMapper;
import online.longzipeng.pojo.dto.UserSearchDTO;
import online.longzipeng.service.UserService;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: lzp
 * @description:
 * @Date: 2022/12/6
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	@Transactional
	@Override
	public void testDynamicDatasource() {
		User user = baseMapper.getUser();
		System.out.println(user.getNickName());

		UserServiceImpl userService = (UserServiceImpl) AopContext.currentProxy();
		userService.getUser();
	}

	@Transactional(propagation = Propagation.REQUIRES_NEW)
//	@DataSource("lzp2")
	@DS("lzp2")
	public void getUser() {
		User user = baseMapper.getUser();
		System.out.println(user.getNickName());
	}


	@Override
	public Page<User> page(UserSearchDTO param) {
		Page<User> page = new Page<>(param.getCurrent(), param.getSize());
		return baseMapper.selectPage(page, Wrappers.<User>lambdaQuery()
				.likeRight(StrUtil.isNotBlank(param.getUsername()), User::getUserName, param.getUsername())
				.eq(ObjectUtil.isNotNull(param.getSex()), User::getSex, param.getSex()));
	}
}
